<template>
  <div class="wrapper" style="margin-bottom: -49px">
    <h2 class="logo">
      <img :src="logo.pic" alt="" />
    </h2>
    <van-form @submit="onSubmit">
      <van-field
        v-model="forgotpass.phone"
        autocomplete="off"
        name="phone"
        label="手机号"
        placeholder="手机号"
        :rules="[
          { required: true, message: '手机号码不能为空' },
          { pattern: phonePattern, message: '手机号的格式有误！请重新输入' },
        ]"
      />
      <van-field
        v-model="forgotpass.captcha"
        name="captcha"
        center
        clearable
        label="短信验证码"
        placeholder="请输入短信验证码"
        :rules="[
          { required: true, message: '请填写短信验证码' },
          { pattern: /^\d{4}$/, message: '验证码必须为4为数字' },
        ]"
      >
        <template #button>
          <van-button
            native-type="button"
            size="small"
            v-if="!toggle"
            @click.stop="countdown"
            :disabled="!show"
            type="primary"
            >发送验证码</van-button
          >
          <van-button size="small" v-else type="warning">
            {{ countnum }} S</van-button
          >
        </template>
      </van-field>
      <van-field
        v-model="forgotpass.password"
        autocomplete="off"
        :type="type"
        name="password"
        :right-icon="type == 'password' ? 'closed-eye' : 'eye-o'"
        @click-right-icon="changeType"
        label="设置新密码"
        placeholder="密码由6-16位数字加字母组成"
        :rules="[
          { required: true, message: '密码不能为空' },
          { pattern: $reg.pwd, message: '密码的格式有误！请重新输入' },
        ]"
      />
      <van-field
        v-model="forgotpass.dbpass"
        :type="type1"
        name="dbpass"
        label="确认密码"
        @click-right-icon="changeType1"
        :right-icon="type1 == 'password' ? 'closed-eye' : 'eye-o'"
        placeholder="6-16位数字加字母"
        :rules="[
          { required: true, message: '请填写确认密码' },
          { validator: checkPassEqual, message: '两次密码输入不一致' },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import obj from '@/utils/mixin'
import { Notify } from 'vant'

export default {
  name: 'register',
  mixins: [obj],
  data () {
    return {
      logo: { pic: require('@/assets/logo.png') },
      forgotpass: {},
      phonePattern: this.$reg.phone,
      type: 'password',
      type1: 'password',
      show: false,
      toggle: false,
      countnum: 30
    }
  },
  watch: {
    forgotpass: {
      deep: true,
      handler (v) {
        if (this.$reg.phone.test(v.phone)) {
          this.show = true
        } else {
          this.show = false
        }
      }
    }
  },
  methods: {
    sendCaptcha () {
      this.$ajax
        .sendcaptcha({
          phone: this.forgotpass.phone
        })
        .then((res) => {
          if (res.code == 200) {
            Notify({ type: 'success', message: res.msg })
          }
        })
    },
    countdown () {
      this.sendCaptcha()
      this.toggle = true
      var timer = setInterval(() => {
        if (this.countnum > 0) {
          this.countnum--
          this.toggle = true
        } else {
          clearInterval(timer)
          timer = null
          this.countnum = 60
          this.toggle = false
        }
      }, 1000)
    },
    onSubmit (value) {
      console.log(value)
      this.$ajax.verifycaptcha(value).then((data) => {
        if (data.code == 200) {
          console.log(data)
          console.log(value)
          this.$ajax.modifypassword(value).then((res) => {
            if (res.code == 200) {
              this.$router.back()
            }
          })
        }
      })
    },
    checkPassEqual (val) {
      return this.forgotpass.password == val
    },
    changeType () {
      this.type = this.type == 'password' ? 'text' : 'password'
    },
    changeType1 () {
      this.type1 = this.type1 == 'password' ? 'text' : 'password'
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  background-color: #fff;
  .logo {
    overflow: hidden;
    text-align: center;
    img {
      margin: 79px auto 40px;
      height: 60px;
      line-height: 60px;
    }
  }
}
.van-button--info {
  margin-top: 40px;
  background-color: #ff5f16;
  border-color: #ff5f16;
}
</style>
